<?php
namespace app\index\model;

use think\Model;

class News extends Model{

    private $newsModel;
    public function __construct(){
        parent::__construct();
        $this -> newsModel = db('news');
    }

    /*  获取资讯数据
     *  @param Array [where]
     *  @return Array [data]
     * 、**/
    public function getNewsDataByWhere($where = false ,$order = 'read_num desc' , $type = 1){
        $data = $this -> newsModel -> where($where) -> order($order) -> find();
        if(empty($data)){   return [];  }
        switch ($type) {
            case 1://首页文章带图
                $dataList = $this -> formarNewsDataforAd($data);
                break;
            case 2://资讯详情页面
                $dataList = $this -> formatNewsDataForDetail($data);
                break;
            default:
                # code...
                break;
        }
        return $dataList;
    }

    /*  获取资讯列表
     *  @param Array [where]
     *  @return Array [data]
     * 、**/
    public function getNewsListByWhere($where = false  , $order = false , $limit = false , $type = 1){
        $data = $this -> newsModel -> where($where) -> order($order) -> limit($limit) -> select();
        if(empty($data)){   return [];  }
        switch ($type) {
            case 1:
                $dataList = $this -> formatNewsListForIndex($data);
                break;
            case 2:
                $dataList = $this -> formatNewsDataForIndexView($data);
                break;
            case 3:
                $dataList = $this -> formatNewsDataForType12List($data);
                break;
            default:
                # code...
                break;
        }
        return $dataList;
    }

    /*  格式化资讯列表数据
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function formatNewsListForIndex($data){
        $cateModel = new Cate();
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['img'] = $value['img'];
            $list[$key]['title'] = $value['title'];
            $list[$key]['type'] = $value['type'];
            $cateName = $cateModel -> getCateValue(['id'=>$value['cate_id']],'name');
            $list[$key]['cname'] = $cateName;
        }
        return $list;
    }

    /*  格式化资讯
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function formarNewsDataforAd($data){
        $list['id'] = $data['id'];
        $list['img'] = $data['img'];
        $list['title'] = $data['title'];
        $list['type'] = $data['type'];
        return $list;
    }
    /*  格式化资讯
     *  @param Array [data]
     *  @return Array [info]
     * **/
    public function formatNewsDataForDetail($data){
        $info['title'] = $data['title'];
        $info['author'] = $data['author'];
        $info['addtime'] = date("Y-m-d H:i:s",$data['addtime']);
        $info['read_num'] = $data['read_num'];
        $info['cate_id'] = $data['cate_id'];
        $info['detail'] = $data['detail'];
        return $info;
    }
    /*  资讯-view数据
     *  @param Array [data]
     *  @return Array [dataList]
     * **/
    public function formatNewsDataForIndexView($data){
        $news = [];
        $ad = [];
        $imgs = [];
        foreach ($data as $key => $value) {
            if($key <2){
                $news[$key]['id'] = $value['id'];
                $news[$key]['title'] = $value['title'];
                $news[$key]['img'] = $value['img'];
                $news[$key]['author'] = $value['author'];
                $news[$key]['sign'] = formatNewsDetailForSign($value['detail'],80);
                $news[$key]['addtime'] = date("Y-m-d",$value['addtime']);
            }
            if($key == 2){
                $ad['id'] = $value['id'];
                $ad['title'] = $value['title'];
                $ad['img'][] = $value['img'];
                $image = explode(',',trim($value['image'],','));
                $ad['img'][] = $image[0];
            }
            if($key == 3){
                $imgs['id'] = $value['id'];
                $imgs['title'] = $value['title'];
                $imgs['img'] = explode(',',trim($value['image'],','));
            }
        }
        $dataList['news'] = $news;
        $dataList['ad'] = $ad;
        $dataList['imgs'] = $imgs;
        return $dataList;
    }
    /*  资讯-下拉加载的数据
     *  @param Array [data]
     *  @return Array [dataList]
     * **/
    public function formatNewsDataForType12List($data){
        foreach ($data as $key => $value) {
            if($key<4){
                $list[$key]['id'] = $value['id'];
                $list[$key]['img'] = $value['img'];
                $list[$key]['title'] = $value['title'];
                $list[$key]['author'] = $value['author'];
                $list[$key]['sign'] = formatNewsDetailForSign($value['detail'],80);
                $list[$key]['addtime'] = date('Y-m-d',$value['addtime']);
            }
            if($key == 4){
                $imgs['id'] = $value['id'];
                $imgs['title'] = $value['title'];
                $imgs['img'] = explode(',',trim($value['image'],','));
            }
        }
        $dataList['list'] = $list;
        if(!empty($imgs)){
            $dataList['imgs'] = $imgs;
        }
        return $dataList;
    }

    /*  执行浏览量+1
     *  @param Array [where]
     *  @return Array [result]
     * **/
    public function fieldAddOne($where,$value){
        $retult = $this -> newsModel -> where($where) -> setInc($value);
        return $retult;
    }


}